package org.company.earth.utils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 常用反射类
 * @author hucw
 *
 */
public class ReflectUtils {
    
    /**
     * 获取clazz中，第一个标有annotationClass的field,找不到返回null
     * @param clazz
     * @param annotationClass
     * @return
     */
    public Field getAnnotationField(Class<?> clazz,
            Class<? extends Annotation> annotationClass) {
        Field[] fields = clazz.getDeclaredFields();
        for(Field field : fields) {
            if(field.isAnnotationPresent(annotationClass)) {
                return field;
            }
        }
        return null;
    }
    
    /**
     * 返回clazz中所有包含annotationClass的字段
     * @param clazz
     * @param annotationClass
     * @return
     */
    public List<Field> getAnnotationFields(Class<?> clazz,
            Class<? extends Annotation> annotationClass) {
        List<Field> list = new ArrayList<Field>();
        Field[] fields = clazz.getDeclaredFields();
        for(Field field : fields) {
            if(field.isAnnotationPresent(annotationClass)) {
                list.add(field);
            }
        }
        return list;
    }
}
